#ifndef RABBITMQCLIENT_H
#define RABBITMQCLIENT_H

#include <QObject>
#include <QTimer>
#include <QTimerEvent>
#include "qamqpclient.h"
#include "qamqpexchange.h"
#include "qamqpqueue.h"

class RabbitMqClient : public QObject
{
    Q_OBJECT
public:
    explicit RabbitMqClient(QObject *parent = nullptr);
    Q_INVOKABLE void start();
    Q_INVOKABLE qint64 request(QString);
    Q_PROPERTY(QString uri READ getUri WRITE setUri)
    Q_PROPERTY(QString servicename READ getServiceName WRITE setServiceName)
    Q_PROPERTY(int timeout READ getTimeout WRITE setTimeout)

public:

    QString getUri() const;
    void setUri(const QString& newUri);

    QString getServiceName() const;
    void setServiceName(const QString& newUri);

    int getTimeout() const;
    void setTimeout(int newTimeout);

signals:
    void requestSignal(qint64,const QString);
    void responseMsg(qint64,const QString&);
    void responseTimeout(qint64&);
    void subscribedMsg(const QString& msg);

protected:
    void timerEvent(QTimerEvent *event);

private slots:
    void clientConnected();

    void topicExchangeDeclared();
    void cmdExchangeDeclared();

    void topicQueueDeclared();
    void cmdQueueDeclared();

    void topicQueueBound();
    void cmdQueueBound();

    void topicMessageReceived();
    void responseReceived();
    void handleRequest(qint64,QString);
private:
    QAmqpClient m_client;
    QString m_uri;
    QString m_servicename;
    QAmqpExchange *m_cmdExchange;
    QAmqpQueue *m_responsecQueue;
    QHash<QString, qint64> m_requestHash;
    QHash<int,QString> m_requestTimerHash;
    int m_timeout;
};

#endif // RABBITMQCLIENT_H
